package com.jjty.dataGenerator.jserji.service;

import com.jjty.dataGenerator.jserji.entity.JsDataAll;
import com.jjty.dataGenerator.jserji.mapper.JsDataAllMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

@Service
public class SchoolNameService {

    private final JsDataAllMapper jsDataAllMapper;

    public SchoolNameService(JsDataAllMapper jsDataAllMapper) {
        this.jsDataAllMapper = jsDataAllMapper;
    }

    @Transactional
    public int fillSchoolNames(String schoolName, int n) {
        // 查询符合条件的数据
        List<JsDataAll> records = jsDataAllMapper.selectEmptySchoolNameInTaizhou();
        
        // 如果符合条件的数据不足n条，取实际数量
        int count = Math.min(n, records.size());
        if (count == 0) {
            return 0;
        }
        
        // 随机选择count条数据
        Random random = new Random();
        for (int i = 0; i < count; i++) {
            int randomIndex = random.nextInt(records.size());
            JsDataAll record = records.get(randomIndex);
            record.setSchoolName(schoolName);
            jsDataAllMapper.updateById(record);
            records.remove(randomIndex); // 移除已选记录，避免重复
        }
        
        return count;
    }
}    